package com.erlang.springmvc.intercept;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @Description：登陆拦截器
 */
public class LoginIntercept implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        HttpSession session = request.getSession();
        Object object = session.getAttribute("customer");
        if (object!=null){
            System.out.println("用户已登录，放行");
            return true;
        }
        System.out.println("用户未登录，去登陆页面");
        response.sendRedirect(request.getContextPath()+"/customer/to-login");
        return false;
    }
}